package com.example.envctel;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class netUtils {
    private static String JYD = "JYD NetUtils ";

    /**
     * 判断是否有网络连接
     *
     * @param context
     * @return
     */
    public static boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 判断WIFI网络是否可用
     *
     * @param context
     * @return
     */
    public static boolean isNetAvailable(Context context) {

        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            //	System.out.println("mWiFiNetworkInfo==null" + mWiFiNetworkInfo == null);
            //	ToastUtils.showToast(context, "mWiFiNetworkInfo==null?" + (mWiFiNetworkInfo == null));
            if (mWiFiNetworkInfo != null && mWiFiNetworkInfo.isAvailable()) {

                return true;
            }
            NetworkInfo mEthernetInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
            //	System.out.println("mEthernetInfo==null" + mEthernetInfo == null);
            //	ToastUtils.showToast(context, "mEthernetInfo==null?" + (mEthernetInfo == null));
            //	ToastUtils.showToast(context, " mEthernetInfo.isAvailable()?" + (mEthernetInfo.isAvailable()));
            if (mEthernetInfo != null) {

                return mEthernetInfo.isAvailable();
            }
        }
        return false;
    }

    /**
     * 获取本地IP wifi下
     *
     * @return
     */
    public static String getLocalIp(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        wifiManager.getDhcpInfo();
        int ipAddress = wifiInfo.getIpAddress();
        if (ipAddress == 0)
            return null;
        return ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "." + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));
    }

    /**
     * 在以太网下获取本地ip
     *
     * @param context
     * @return
     */
    public static String getLocalIpByEthernet(Context context) {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    //
                    if (!inetAddress.isLoopbackAddress()) {
                        if (inetAddress.getHostAddress().toString().length() <= 15) {
                            return inetAddress.getHostAddress().toString();
                        }
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return "";
    }
}
